#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2023/3/9 20:07
# @Author  : WuJunQi
# @Email   : 1479652129@qq.com
# @File    : People.py
# @Software: PyCharm
import math


class Peoples:
    def __init__(self):
        self.frame = 0
        self.nowtime = 0
        self.Old_Active = {}
        self.Active = {}
        # 目标的函数
        self.object_1 = Point()
        self.object_2 = Point()
        self.want_to_cross_id = {}
        self.want_to_cross_id[0] = 0
    def addnew(self,id,x,y):
        self.Active[id] = Point(x,y)

    def set_Zebra_crossing_edge(self,x1,y1,x2,y2):
        self.object_1.x = x1
        self.object_1.y = y1
        self.object_2.x = x2
        self.object_2.y = y2
    def count_crossnum(self,id):            # 检测过之前
        if id not in self.Active:
            return None
        if id in self.Old_Active:
            if is_pointing(self.Old_Active[id],self.Active[id],self.object_1,self.object_2):
                # 加入过街行列
                self.want_to_cross_id[self.frame] += 1
                self.Old_Active[id] = self.Active[id]
                return True
        # 更新旧的点
        self.Old_Active[id] = self.Active[id]
        return False
    def clearframe(self):
        self.frame += 1
        self.want_to_cross_id[self.frame] = 0
        self.Active.clear()


class Point:
    def __init__(self,x = 0,y = 0):
        self.x = x
        self.y = y

# 一个点到两个点构成线段的距离
def one_to_line_distance(myPoint,line_start_Point,line_end_Point):
    return abs((line_end_Point.y - line_start_Point.y)*myPoint.x - (line_end_Point.x - line_start_Point.x)*myPoint.y + line_end_Point.x*line_start_Point.y - line_end_Point.y*line_start_Point.x) / math.sqrt((line_end_Point.y - line_start_Point.y)**2 + (line_end_Point.x - line_start_Point.x)**2)

def get_distance(point1,point2):
    return math.sqrt((point1.x - point2.x)**2 + (point1.y - point2.y)**2)

# 判断一个向量是否指向一个边
def is_pointing(startPoint, endPoint,line_start_point,line_end_point):
    if one_to_line_distance(startPoint,line_start_point,line_end_point)+10 > one_to_line_distance(endPoint,line_start_point,line_end_point):
        return True
    else:
        return False


